<?php



namespace App\Http\Requests\ApiV2;

use App\Services\Business\Models\UserConquer;
use App\Services\Member\Models\Activity;
use Illuminate\Support\Facades\Auth;

class ActivityRequest extends BaseRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'title' => 'required|max:120',

        ];
    }

    public function messages()
    {
        return [
            'title.required' => __('请输入标题'),
            'title.max' => __('课程标题的长度不能超过:size个字符', ['size' => 120]),

        ];
    }

    public function filldata()
    {
        $conquerId =(int)$this->input('conquer_id');
        $userId= Auth::id();
        $activity =  Activity::query()->where('user_id',$userId)->where('conquer_id',$conquerId)->whereDate('created_at', '<', now()->startOfDay())->latest()->first();
        $record  =  UserConquer::query()->where('user_id',$userId)->where('conquer_id',$conquerId)->first();
        return [
            'user_id'=>$userId,
            'title' => (string)$this->input('title'),
            'conquer_id' => $conquerId,
            'is_show' =>(int) $this->input('is_show',1),
            'images' =>(array) $this->input('images',[]),
            'video_ids' =>(array) $this->input('video_ids',[]),
            'weight'=> $this->input('weight'),
            'last_different_weight' => $activity ? $activity['weight'] - $this->input('weight') :$record['weight'] -$this->input('weight') ,
            'total_different_weight'=>   $record['weight'] - $this->input('weight') ,
[]
        ];
    }
}
